package com.ldzy.controller;


import com.ldzy.pojo.Book;
import com.ldzy.pojo.SearchBookVO;
import com.ldzy.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/books")
@RestController
public class BookController {
    //自动注入:根据属性的名字到spring容器中去寻找有没有这个名字对象
    //自动注入:根据属性的类型到spring容器中去寻找有没有这种类型的对象
    @Autowired
    private BookService bookService;

    //搜索控制器
    @PostMapping("/search")
    public MyResponse search(@RequestBody SearchBookVO searchBookVO){
        String searchType,searchWord;
        searchType=searchBookVO.getSearchType();
        searchWord=searchBookVO.getSearchWord();
        log.info(searchType);
        //根据前端发来数据进行查询
        if (searchType.equals("书名")){
            Book book=new Book();
            book.setBook_title(searchWord);
            List<Book> books=bookService.getBooks(book);
            return new MyResponse(ResultCode.SUCCESS_QUERY,books);
        }else if (searchType.equals("作者")){
            Book book=new Book();
            book.setBook_author(searchWord);
            List<Book> books=bookService.getBooks(book);
            return new MyResponse(ResultCode.SUCCESS_QUERY,books);
        }else {
            return new MyResponse(ResultCode.ERROR_QUERY_GOODS);
        }
    }
    //查一个
    @GetMapping("/{id}")
    public MyResponse getBookById(@Validated Book book){
        log.info("info:"+book.getId());
        Book book1=bookService.getBookById(book.getId());
        return new MyResponse(ResultCode.SUCCESS_QUERY,book1);
    }

    //删除
    @DeleteMapping("/{id}")
    public MyResponse delBook(@Validated Book book){
        log.info("info:"+book.getId());
        return new MyResponse(ResultCode.SUCCESS_DELETE,true);
    }

    //增
    @PostMapping()
    public MyResponse addBook(@RequestBody Book book){
        log.info(book.getBook_title());
        return new MyResponse(ResultCode.SUCCESS_ADD,true);
    }
//查所有
    @GetMapping()
    public MyResponse getAllBooks(){
        log.info("getAllBooks");
        List<Book> books=new ArrayList<Book>(5);
        Book book1 = new Book();
        book1.setId(1);
        book1.setBook_title("vue.js");
        books.add(book1);
        Book book2 = new Book();
        book2.setId(2);
        book2.setBook_title("ssm");
        books.add(book2);
        return new MyResponse(ResultCode.SUCCESS_QUERY,books);
    }
//查部分:根据书名来查
    @GetMapping("/name/{name}")
    public MyResponse getBooksByName(@PathVariable("name") String name){
        log.info("getBooks:"+name);
        List<Book> books=new ArrayList<Book>(5);
        Book book1 = new Book();
        book1.setId(1);
        book1.setBook_title("vue.js");
        books.add(book1);
        Book book2 = new Book();
        book2.setId(2);
        book2.setBook_title("ssm");
        books.add(book2);
        return new MyResponse(ResultCode.SUCCESS_QUERY,books);
    }

    //修改
    @PutMapping()
    public MyResponse editBook(@RequestBody @Validated Book book){
        log.info(book.getBook_title());
        return new MyResponse(ResultCode.SUCCESS_UPDATE,true);
    }



}
